import axios from '~/axios'

// 传入 tableStyle 数组，返回以字段为 key 的 map
export function getCtlTypeMap(tableStyleList){
  if(!tableStyleList || tableStyleList.length === 0) return null
  const ret = {}
  tableStyleList.forEach(item => {
    let selectOpts = null
    if(item.opts && item.opts.length > 0 && item.opts !== 'undefined') {
      selectOpts = JSON.parse(item.opts)
    }

    ret[item.fieldName] = {}
    ret[item.fieldName].record = item
    ret[item.fieldName].selectOpts = selectOpts
  })

  return ret
}

export async function getRemoteData(tableStyleList){
  const selectCtls = tableStyleList.filter(item => item.ctlType === 'select')
  if(!selectCtls || selectCtls.length === 0) return null

  const ret = {}
  for (const tableStyleRecord of selectCtls) {
    if(!tableStyleRecord.opts || tableStyleRecord.opts.length === 0) continue
    const config = JSON.parse(tableStyleRecord.opts)
    // const res = await axios.post(config.api.url, config.api.data)
    const res = await axios({...config.api})
    if(res.code === 1) {
      ret[tableStyleRecord.fieldName] = {}
      ret[tableStyleRecord.fieldName] = res.data
    }
  }
  return ret
}